package com.itcam.phaseii.generic;

import java.util.ArrayList;

public class Generic01 {
    public static void main(String[] args) {
        // 使用传统的方法来解决
        ArrayList arrayList = new ArrayList();
        arrayList.add(new Dog("昊天", 10));
        arrayList.add(new Dog("小白", 10));
        arrayList.add(new Dog("小黑", 12));

        // 假如我们的程序员，不小心，添加了一只猫
        arrayList.add(new Cat("橘宝", 1));

        // 遍历
        for (Object o : arrayList) {
            // 向下转型Object->Dog
            Dog dog = (Dog) o;
            System.out.println(dog.getName() + "——" + dog.getAge());
        }

    }
}

class Dog {
    private String name;
    private int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

class Cat {
    private String name;
    private int age;

    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}